{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5a67ca73-04e8-46c2-bb83-5c02dbe15abb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: http://mirrors.aliyun.com/pypi/simple\n",
      "Requirement already satisfied: pandas in /root/miniconda3/lib/python3.8/site-packages (2.0.3)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /root/miniconda3/lib/python3.8/site-packages (from pandas) (2.8.2)\n",
      "Requirement already satisfied: tzdata>=2022.1 in /root/miniconda3/lib/python3.8/site-packages (from pandas) (2024.1)\n",
      "Requirement already satisfied: pytz>=2020.1 in /root/miniconda3/lib/python3.8/site-packages (from pandas) (2021.1)\n",
      "Requirement already satisfied: numpy>=1.20.3 in /root/miniconda3/lib/python3.8/site-packages (from pandas) (1.21.2)\n",
      "Requirement already satisfied: six>=1.5 in /root/miniconda3/lib/python3.8/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "# ! pip install pandas\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import pandas as pd\n",
    "from torch.utils.data import Dataset, DataLoader\n",
    "import torch.nn.functional as F\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "19ae53ec-aa1f-40bc-bd0f-f39aa868019c",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 定义LSTM模型\n",
    "class LSTMModel(nn.Module):\n",
    "    def __init__(self, input_size, hidden_size, num_layers, num_classes):\n",
    "        super(LSTMModel, self).__init__()\n",
    "        self.hidden_size = hidden_size\n",
    "        self.num_layers = num_layers\n",
    "        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)\n",
    "        self.fc = nn.Linear(hidden_size, num_classes)\n",
    "\n",
    "    def forward(self, x):\n",
    "        h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)\n",
    "        c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)\n",
    "        out, _ = self.lstm(x, (h0, c0))\n",
    "        out = self.fc(out[:, -1, :])\n",
    "        return out\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6b2b7f8c-3a21-4db7-a521-abd94a361555",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 自定义数据集类\n",
    "class CustomDataset(Dataset):\n",
    "    def __init__(self, csv_file):\n",
    "        self.data = pd.read_csv(csv_file)\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.data)\n",
    "\n",
    "    def __getitem__(self, idx):\n",
    "        sequence = list(map(int, str(self.data.iloc[idx, 0]).split(', '))) # 将字符串序列转换为整数列表\n",
    "        label = int(self.data.iloc[idx, 1]) # 获取标签\n",
    "        return torch.tensor(sequence), torch.tensor(label)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "03c95726-9acf-4a98-9555-c2e331e2df30",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 超参数设置\n",
    "input_size = 1 # 输入特征维度（每个行为序列中的元素个数）\n",
    "hidden_size = 128 # LSTM隐藏单元数\n",
    "num_layers = 2 # LSTM层数\n",
    "num_classes = 3 # 分类类别数\n",
    "batch_size = 4\n",
    "num_epochs = 100\n",
    "learning_rate = 0.0001\n",
    "\n",
    "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "999f1cd1-e1d3-4bf1-936e-7f5772caef48",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "def collate_fn(batch):\n",
    "    sequences, labels = zip(*batch)\n",
    "    # 找到最长的序列长度\n",
    "    max_length = max([len(seq) for seq in sequences])\n",
    "    # 填充序列\n",
    "    sequences_padded = [torch.cat([seq, torch.zeros(max_length - len(seq)).long()]) for seq in sequences]\n",
    "    return torch.stack(sequences_padded, dim=0), torch.tensor(labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "089a9ce6-2f6a-4439-9985-0d97b02dc655",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 加载数据集\n",
    "dataset = CustomDataset('/root/LSTM2/train2.csv')  # 修改为Excel文件的路径\n",
    "\n",
    "\n",
    "\n",
    "loader = DataLoader(dataset, batch_size=batch_size, shuffle=True, collate_fn=collate_fn)\n",
    "\n",
    "# loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "574c04dd-945c-4af9-b031-7f6a098d2b2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 初始化模型、损失函数和优化器\n",
    "model = LSTMModel(input_size, hidden_size, num_layers, num_classes).to(device)\n",
    "criterion = nn.CrossEntropyLoss()\n",
    "optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "26249f47-2762-4eba-93c8-4e79c1e1ad91",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch [1/100], Step [20/99], Loss: 1.1236\n",
      "Epoch [1/100], Step [40/99], Loss: 1.0281\n",
      "Epoch [1/100], Step [60/99], Loss: 0.9797\n",
      "Epoch [1/100], Step [80/99], Loss: 1.1297\n",
      "Epoch [2/100], Step [20/99], Loss: 1.0343\n",
      "Epoch [2/100], Step [40/99], Loss: 0.8535\n",
      "Epoch [2/100], Step [60/99], Loss: 0.9119\n",
      "Epoch [2/100], Step [80/99], Loss: 0.8011\n",
      "Epoch [3/100], Step [20/99], Loss: 0.6659\n",
      "Epoch [3/100], Step [40/99], Loss: 0.7713\n",
      "Epoch [3/100], Step [60/99], Loss: 0.4775\n",
      "Epoch [3/100], Step [80/99], Loss: 0.6173\n",
      "Epoch [4/100], Step [20/99], Loss: 0.7576\n",
      "Epoch [4/100], Step [40/99], Loss: 0.5804\n",
      "Epoch [4/100], Step [60/99], Loss: 1.1224\n",
      "Epoch [4/100], Step [80/99], Loss: 0.3367\n",
      "Epoch [5/100], Step [20/99], Loss: 1.0516\n",
      "Epoch [5/100], Step [40/99], Loss: 0.7006\n",
      "Epoch [5/100], Step [60/99], Loss: 0.3435\n",
      "Epoch [5/100], Step [80/99], Loss: 1.0352\n",
      "Epoch [6/100], Step [20/99], Loss: 0.8606\n",
      "Epoch [6/100], Step [40/99], Loss: 0.9172\n",
      "Epoch [6/100], Step [60/99], Loss: 0.7254\n",
      "Epoch [6/100], Step [80/99], Loss: 0.3094\n",
      "Epoch [7/100], Step [20/99], Loss: 0.9487\n",
      "Epoch [7/100], Step [40/99], Loss: 0.5457\n",
      "Epoch [7/100], Step [60/99], Loss: 0.4830\n",
      "Epoch [7/100], Step [80/99], Loss: 0.6017\n",
      "Epoch [8/100], Step [20/99], Loss: 0.5662\n",
      "Epoch [8/100], Step [40/99], Loss: 0.2889\n",
      "Epoch [8/100], Step [60/99], Loss: 0.8770\n",
      "Epoch [8/100], Step [80/99], Loss: 0.4970\n",
      "Epoch [9/100], Step [20/99], Loss: 1.6242\n",
      "Epoch [9/100], Step [40/99], Loss: 1.3197\n",
      "Epoch [9/100], Step [60/99], Loss: 0.3229\n",
      "Epoch [9/100], Step [80/99], Loss: 0.6601\n",
      "Epoch [10/100], Step [20/99], Loss: 0.2413\n",
      "Epoch [10/100], Step [40/99], Loss: 0.8840\n",
      "Epoch [10/100], Step [60/99], Loss: 0.6271\n",
      "Epoch [10/100], Step [80/99], Loss: 1.0208\n",
      "Epoch [11/100], Step [20/99], Loss: 1.4109\n",
      "Epoch [11/100], Step [40/99], Loss: 0.5311\n",
      "Epoch [11/100], Step [60/99], Loss: 0.8672\n",
      "Epoch [11/100], Step [80/99], Loss: 0.9526\n",
      "Epoch [12/100], Step [20/99], Loss: 0.4515\n",
      "Epoch [12/100], Step [40/99], Loss: 0.5199\n",
      "Epoch [12/100], Step [60/99], Loss: 0.9441\n",
      "Epoch [12/100], Step [80/99], Loss: 0.2993\n",
      "Epoch [13/100], Step [20/99], Loss: 0.2747\n",
      "Epoch [13/100], Step [40/99], Loss: 1.5418\n",
      "Epoch [13/100], Step [60/99], Loss: 0.9674\n",
      "Epoch [13/100], Step [80/99], Loss: 1.2420\n",
      "Epoch [14/100], Step [20/99], Loss: 0.7574\n",
      "Epoch [14/100], Step [40/99], Loss: 1.1183\n",
      "Epoch [14/100], Step [60/99], Loss: 0.5341\n",
      "Epoch [14/100], Step [80/99], Loss: 0.3234\n",
      "Epoch [15/100], Step [20/99], Loss: 0.5912\n",
      "Epoch [15/100], Step [40/99], Loss: 0.6752\n",
      "Epoch [15/100], Step [60/99], Loss: 0.2493\n",
      "Epoch [15/100], Step [80/99], Loss: 0.6405\n",
      "Epoch [16/100], Step [20/99], Loss: 0.2467\n",
      "Epoch [16/100], Step [40/99], Loss: 1.9737\n",
      "Epoch [16/100], Step [60/99], Loss: 0.3408\n",
      "Epoch [16/100], Step [80/99], Loss: 0.7832\n",
      "Epoch [17/100], Step [20/99], Loss: 0.6589\n",
      "Epoch [17/100], Step [40/99], Loss: 1.0024\n",
      "Epoch [17/100], Step [60/99], Loss: 0.8923\n",
      "Epoch [17/100], Step [80/99], Loss: 0.6418\n",
      "Epoch [18/100], Step [20/99], Loss: 1.0304\n",
      "Epoch [18/100], Step [40/99], Loss: 0.5385\n",
      "Epoch [18/100], Step [60/99], Loss: 0.3719\n",
      "Epoch [18/100], Step [80/99], Loss: 0.5586\n",
      "Epoch [19/100], Step [20/99], Loss: 0.2332\n",
      "Epoch [19/100], Step [40/99], Loss: 0.5396\n",
      "Epoch [19/100], Step [60/99], Loss: 0.7700\n",
      "Epoch [19/100], Step [80/99], Loss: 1.2117\n",
      "Epoch [20/100], Step [20/99], Loss: 0.5914\n",
      "Epoch [20/100], Step [40/99], Loss: 0.8626\n",
      "Epoch [20/100], Step [60/99], Loss: 0.7492\n",
      "Epoch [20/100], Step [80/99], Loss: 1.2020\n",
      "Epoch [21/100], Step [20/99], Loss: 0.9786\n",
      "Epoch [21/100], Step [40/99], Loss: 0.8776\n",
      "Epoch [21/100], Step [60/99], Loss: 0.3334\n",
      "Epoch [21/100], Step [80/99], Loss: 0.6758\n",
      "Epoch [22/100], Step [20/99], Loss: 0.4730\n",
      "Epoch [22/100], Step [40/99], Loss: 0.4332\n",
      "Epoch [22/100], Step [60/99], Loss: 0.3500\n",
      "Epoch [22/100], Step [80/99], Loss: 0.8492\n",
      "Epoch [23/100], Step [20/99], Loss: 1.3259\n",
      "Epoch [23/100], Step [40/99], Loss: 0.2856\n",
      "Epoch [23/100], Step [60/99], Loss: 0.4619\n",
      "Epoch [23/100], Step [80/99], Loss: 0.7436\n",
      "Epoch [24/100], Step [20/99], Loss: 0.3536\n",
      "Epoch [24/100], Step [40/99], Loss: 1.0478\n",
      "Epoch [24/100], Step [60/99], Loss: 1.0019\n",
      "Epoch [24/100], Step [80/99], Loss: 0.2236\n",
      "Epoch [25/100], Step [20/99], Loss: 0.7780\n",
      "Epoch [25/100], Step [40/99], Loss: 0.7557\n",
      "Epoch [25/100], Step [60/99], Loss: 0.3078\n",
      "Epoch [25/100], Step [80/99], Loss: 1.2579\n",
      "Epoch [26/100], Step [20/99], Loss: 0.5780\n",
      "Epoch [26/100], Step [40/99], Loss: 0.4929\n",
      "Epoch [26/100], Step [60/99], Loss: 0.4692\n",
      "Epoch [26/100], Step [80/99], Loss: 1.1877\n",
      "Epoch [27/100], Step [20/99], Loss: 1.3979\n",
      "Epoch [27/100], Step [40/99], Loss: 0.4682\n",
      "Epoch [27/100], Step [60/99], Loss: 0.2575\n",
      "Epoch [27/100], Step [80/99], Loss: 0.4156\n",
      "Epoch [28/100], Step [20/99], Loss: 0.2077\n",
      "Epoch [28/100], Step [40/99], Loss: 0.6344\n",
      "Epoch [28/100], Step [60/99], Loss: 1.2960\n",
      "Epoch [28/100], Step [80/99], Loss: 0.2122\n",
      "Epoch [29/100], Step [20/99], Loss: 0.6816\n",
      "Epoch [29/100], Step [40/99], Loss: 0.2458\n",
      "Epoch [29/100], Step [60/99], Loss: 0.4074\n",
      "Epoch [29/100], Step [80/99], Loss: 1.4898\n",
      "Epoch [30/100], Step [20/99], Loss: 0.9198\n",
      "Epoch [30/100], Step [40/99], Loss: 0.5322\n",
      "Epoch [30/100], Step [60/99], Loss: 1.0763\n",
      "Epoch [30/100], Step [80/99], Loss: 0.9434\n",
      "Epoch [31/100], Step [20/99], Loss: 0.4559\n",
      "Epoch [31/100], Step [40/99], Loss: 1.3077\n",
      "Epoch [31/100], Step [60/99], Loss: 1.1729\n",
      "Epoch [31/100], Step [80/99], Loss: 0.5609\n",
      "Epoch [32/100], Step [20/99], Loss: 0.5699\n",
      "Epoch [32/100], Step [40/99], Loss: 0.3883\n",
      "Epoch [32/100], Step [60/99], Loss: 1.0485\n",
      "Epoch [32/100], Step [80/99], Loss: 0.4363\n",
      "Epoch [33/100], Step [20/99], Loss: 0.5591\n",
      "Epoch [33/100], Step [40/99], Loss: 0.7098\n",
      "Epoch [33/100], Step [60/99], Loss: 0.4162\n",
      "Epoch [33/100], Step [80/99], Loss: 1.0039\n",
      "Epoch [34/100], Step [20/99], Loss: 1.2455\n",
      "Epoch [34/100], Step [40/99], Loss: 0.6872\n",
      "Epoch [34/100], Step [60/99], Loss: 0.3837\n",
      "Epoch [34/100], Step [80/99], Loss: 0.5137\n",
      "Epoch [35/100], Step [20/99], Loss: 0.8013\n",
      "Epoch [35/100], Step [40/99], Loss: 0.6481\n",
      "Epoch [35/100], Step [60/99], Loss: 0.3497\n",
      "Epoch [35/100], Step [80/99], Loss: 0.2520\n",
      "Epoch [36/100], Step [20/99], Loss: 0.7738\n",
      "Epoch [36/100], Step [40/99], Loss: 0.6806\n",
      "Epoch [36/100], Step [60/99], Loss: 0.6850\n",
      "Epoch [36/100], Step [80/99], Loss: 0.1750\n",
      "Epoch [37/100], Step [20/99], Loss: 1.0720\n",
      "Epoch [37/100], Step [40/99], Loss: 1.5032\n",
      "Epoch [37/100], Step [60/99], Loss: 0.1836\n",
      "Epoch [37/100], Step [80/99], Loss: 0.1460\n",
      "Epoch [38/100], Step [20/99], Loss: 0.9861\n",
      "Epoch [38/100], Step [40/99], Loss: 0.2850\n",
      "Epoch [38/100], Step [60/99], Loss: 0.5280\n",
      "Epoch [38/100], Step [80/99], Loss: 0.6691\n",
      "Epoch [39/100], Step [20/99], Loss: 0.5280\n",
      "Epoch [39/100], Step [40/99], Loss: 1.2768\n",
      "Epoch [39/100], Step [60/99], Loss: 0.2974\n",
      "Epoch [39/100], Step [80/99], Loss: 0.5134\n",
      "Epoch [40/100], Step [20/99], Loss: 0.3100\n",
      "Epoch [40/100], Step [40/99], Loss: 0.7518\n",
      "Epoch [40/100], Step [60/99], Loss: 0.6717\n",
      "Epoch [40/100], Step [80/99], Loss: 0.3538\n",
      "Epoch [41/100], Step [20/99], Loss: 0.4958\n",
      "Epoch [41/100], Step [40/99], Loss: 0.2511\n",
      "Epoch [41/100], Step [60/99], Loss: 0.7325\n",
      "Epoch [41/100], Step [80/99], Loss: 0.3466\n",
      "Epoch [42/100], Step [20/99], Loss: 0.6248\n",
      "Epoch [42/100], Step [40/99], Loss: 0.7922\n",
      "Epoch [42/100], Step [60/99], Loss: 0.6374\n",
      "Epoch [42/100], Step [80/99], Loss: 0.3277\n",
      "Epoch [43/100], Step [20/99], Loss: 0.2919\n",
      "Epoch [43/100], Step [40/99], Loss: 0.5004\n",
      "Epoch [43/100], Step [60/99], Loss: 0.5245\n",
      "Epoch [43/100], Step [80/99], Loss: 0.7753\n",
      "Epoch [44/100], Step [20/99], Loss: 1.0876\n",
      "Epoch [44/100], Step [40/99], Loss: 0.7328\n",
      "Epoch [44/100], Step [60/99], Loss: 0.6400\n",
      "Epoch [44/100], Step [80/99], Loss: 0.6030\n",
      "Epoch [45/100], Step [20/99], Loss: 1.1050\n",
      "Epoch [45/100], Step [40/99], Loss: 0.8009\n",
      "Epoch [45/100], Step [60/99], Loss: 0.5109\n",
      "Epoch [45/100], Step [80/99], Loss: 0.7445\n",
      "Epoch [46/100], Step [20/99], Loss: 0.2109\n",
      "Epoch [46/100], Step [40/99], Loss: 0.6343\n",
      "Epoch [46/100], Step [60/99], Loss: 0.2905\n",
      "Epoch [46/100], Step [80/99], Loss: 0.6023\n",
      "Epoch [47/100], Step [20/99], Loss: 0.7393\n",
      "Epoch [47/100], Step [40/99], Loss: 0.5769\n",
      "Epoch [47/100], Step [60/99], Loss: 1.1909\n",
      "Epoch [47/100], Step [80/99], Loss: 0.7010\n",
      "Epoch [48/100], Step [20/99], Loss: 0.3198\n",
      "Epoch [48/100], Step [40/99], Loss: 0.2904\n",
      "Epoch [48/100], Step [60/99], Loss: 0.5138\n",
      "Epoch [48/100], Step [80/99], Loss: 1.3258\n",
      "Epoch [49/100], Step [20/99], Loss: 0.2513\n",
      "Epoch [49/100], Step [40/99], Loss: 0.8122\n",
      "Epoch [49/100], Step [60/99], Loss: 0.5286\n",
      "Epoch [49/100], Step [80/99], Loss: 0.8193\n",
      "Epoch [50/100], Step [20/99], Loss: 1.0009\n",
      "Epoch [50/100], Step [40/99], Loss: 0.2864\n",
      "Epoch [50/100], Step [60/99], Loss: 0.9722\n",
      "Epoch [50/100], Step [80/99], Loss: 0.6221\n",
      "Epoch [51/100], Step [20/99], Loss: 0.7758\n",
      "Epoch [51/100], Step [40/99], Loss: 0.5807\n",
      "Epoch [51/100], Step [60/99], Loss: 2.3903\n",
      "Epoch [51/100], Step [80/99], Loss: 0.5009\n",
      "Epoch [52/100], Step [20/99], Loss: 0.2200\n",
      "Epoch [52/100], Step [40/99], Loss: 0.4028\n",
      "Epoch [52/100], Step [60/99], Loss: 0.5930\n",
      "Epoch [52/100], Step [80/99], Loss: 0.7915\n",
      "Epoch [53/100], Step [20/99], Loss: 0.1323\n",
      "Epoch [53/100], Step [40/99], Loss: 0.5271\n",
      "Epoch [53/100], Step [60/99], Loss: 0.5641\n",
      "Epoch [53/100], Step [80/99], Loss: 1.3424\n",
      "Epoch [54/100], Step [20/99], Loss: 0.7303\n",
      "Epoch [54/100], Step [40/99], Loss: 0.1841\n",
      "Epoch [54/100], Step [60/99], Loss: 0.2552\n",
      "Epoch [54/100], Step [80/99], Loss: 0.2845\n",
      "Epoch [55/100], Step [20/99], Loss: 0.4534\n",
      "Epoch [55/100], Step [40/99], Loss: 0.4166\n",
      "Epoch [55/100], Step [60/99], Loss: 1.1106\n",
      "Epoch [55/100], Step [80/99], Loss: 0.9193\n",
      "Epoch [56/100], Step [20/99], Loss: 0.7685\n",
      "Epoch [56/100], Step [40/99], Loss: 0.2591\n",
      "Epoch [56/100], Step [60/99], Loss: 0.6082\n",
      "Epoch [56/100], Step [80/99], Loss: 1.5114\n",
      "Epoch [57/100], Step [20/99], Loss: 0.1007\n",
      "Epoch [57/100], Step [40/99], Loss: 0.1313\n",
      "Epoch [57/100], Step [60/99], Loss: 0.2039\n",
      "Epoch [57/100], Step [80/99], Loss: 0.4975\n",
      "Epoch [58/100], Step [20/99], Loss: 0.7759\n",
      "Epoch [58/100], Step [40/99], Loss: 0.1434\n",
      "Epoch [58/100], Step [60/99], Loss: 0.8265\n",
      "Epoch [58/100], Step [80/99], Loss: 0.3246\n",
      "Epoch [59/100], Step [20/99], Loss: 0.7788\n",
      "Epoch [59/100], Step [40/99], Loss: 0.1525\n",
      "Epoch [59/100], Step [60/99], Loss: 0.7624\n",
      "Epoch [59/100], Step [80/99], Loss: 0.9036\n",
      "Epoch [60/100], Step [20/99], Loss: 0.5324\n",
      "Epoch [60/100], Step [40/99], Loss: 0.2480\n",
      "Epoch [60/100], Step [60/99], Loss: 0.2141\n",
      "Epoch [60/100], Step [80/99], Loss: 0.5415\n",
      "Epoch [61/100], Step [20/99], Loss: 0.7772\n",
      "Epoch [61/100], Step [40/99], Loss: 0.6515\n",
      "Epoch [61/100], Step [60/99], Loss: 0.4929\n",
      "Epoch [61/100], Step [80/99], Loss: 0.3885\n",
      "Epoch [62/100], Step [20/99], Loss: 0.7310\n",
      "Epoch [62/100], Step [40/99], Loss: 0.1610\n",
      "Epoch [62/100], Step [60/99], Loss: 0.6685\n",
      "Epoch [62/100], Step [80/99], Loss: 0.4820\n",
      "Epoch [63/100], Step [20/99], Loss: 0.3273\n",
      "Epoch [63/100], Step [40/99], Loss: 0.3540\n",
      "Epoch [63/100], Step [60/99], Loss: 0.2994\n",
      "Epoch [63/100], Step [80/99], Loss: 1.3894\n",
      "Epoch [64/100], Step [20/99], Loss: 0.5648\n",
      "Epoch [64/100], Step [40/99], Loss: 0.9357\n",
      "Epoch [64/100], Step [60/99], Loss: 0.4642\n",
      "Epoch [64/100], Step [80/99], Loss: 0.1021\n",
      "Epoch [65/100], Step [20/99], Loss: 0.6330\n",
      "Epoch [65/100], Step [40/99], Loss: 0.8376\n",
      "Epoch [65/100], Step [60/99], Loss: 0.3953\n",
      "Epoch [65/100], Step [80/99], Loss: 0.0535\n",
      "Epoch [66/100], Step [20/99], Loss: 0.5544\n",
      "Epoch [66/100], Step [40/99], Loss: 0.3706\n",
      "Epoch [66/100], Step [60/99], Loss: 0.9316\n",
      "Epoch [66/100], Step [80/99], Loss: 0.8748\n",
      "Epoch [67/100], Step [20/99], Loss: 1.2760\n",
      "Epoch [67/100], Step [40/99], Loss: 1.3008\n",
      "Epoch [67/100], Step [60/99], Loss: 0.4558\n",
      "Epoch [67/100], Step [80/99], Loss: 0.5315\n",
      "Epoch [68/100], Step [20/99], Loss: 0.2680\n",
      "Epoch [68/100], Step [40/99], Loss: 0.1393\n",
      "Epoch [68/100], Step [60/99], Loss: 0.4365\n",
      "Epoch [68/100], Step [80/99], Loss: 0.8450\n",
      "Epoch [69/100], Step [20/99], Loss: 0.3428\n",
      "Epoch [69/100], Step [40/99], Loss: 0.4632\n",
      "Epoch [69/100], Step [60/99], Loss: 0.9065\n",
      "Epoch [69/100], Step [80/99], Loss: 0.1269\n",
      "Epoch [70/100], Step [20/99], Loss: 0.2229\n",
      "Epoch [70/100], Step [40/99], Loss: 0.9989\n",
      "Epoch [70/100], Step [60/99], Loss: 0.5716\n",
      "Epoch [70/100], Step [80/99], Loss: 0.3328\n",
      "Epoch [71/100], Step [20/99], Loss: 0.3871\n",
      "Epoch [71/100], Step [40/99], Loss: 0.3043\n",
      "Epoch [71/100], Step [60/99], Loss: 0.2741\n",
      "Epoch [71/100], Step [80/99], Loss: 0.1814\n",
      "Epoch [72/100], Step [20/99], Loss: 0.6454\n",
      "Epoch [72/100], Step [40/99], Loss: 0.3210\n",
      "Epoch [72/100], Step [60/99], Loss: 0.5044\n",
      "Epoch [72/100], Step [80/99], Loss: 0.4265\n",
      "Epoch [73/100], Step [20/99], Loss: 0.4514\n",
      "Epoch [73/100], Step [40/99], Loss: 0.2851\n",
      "Epoch [73/100], Step [60/99], Loss: 0.3856\n",
      "Epoch [73/100], Step [80/99], Loss: 0.3880\n",
      "Epoch [74/100], Step [20/99], Loss: 0.0898\n",
      "Epoch [74/100], Step [40/99], Loss: 0.6777\n",
      "Epoch [74/100], Step [60/99], Loss: 0.1798\n",
      "Epoch [74/100], Step [80/99], Loss: 0.1648\n",
      "Epoch [75/100], Step [20/99], Loss: 0.2261\n",
      "Epoch [75/100], Step [40/99], Loss: 0.5772\n",
      "Epoch [75/100], Step [60/99], Loss: 1.4722\n",
      "Epoch [75/100], Step [80/99], Loss: 0.9080\n",
      "Epoch [76/100], Step [20/99], Loss: 0.4436\n",
      "Epoch [76/100], Step [40/99], Loss: 0.9128\n",
      "Epoch [76/100], Step [60/99], Loss: 0.6548\n",
      "Epoch [76/100], Step [80/99], Loss: 0.5055\n",
      "Epoch [77/100], Step [20/99], Loss: 0.2644\n",
      "Epoch [77/100], Step [40/99], Loss: 0.5018\n",
      "Epoch [77/100], Step [60/99], Loss: 0.1520\n",
      "Epoch [77/100], Step [80/99], Loss: 0.2474\n",
      "Epoch [78/100], Step [20/99], Loss: 0.6846\n",
      "Epoch [78/100], Step [40/99], Loss: 0.0585\n",
      "Epoch [78/100], Step [60/99], Loss: 0.3861\n",
      "Epoch [78/100], Step [80/99], Loss: 0.1602\n",
      "Epoch [79/100], Step [20/99], Loss: 1.0260\n",
      "Epoch [79/100], Step [40/99], Loss: 0.1428\n",
      "Epoch [79/100], Step [60/99], Loss: 0.6605\n",
      "Epoch [79/100], Step [80/99], Loss: 0.2710\n",
      "Epoch [80/100], Step [20/99], Loss: 0.4353\n",
      "Epoch [80/100], Step [40/99], Loss: 0.7032\n",
      "Epoch [80/100], Step [60/99], Loss: 0.3575\n",
      "Epoch [80/100], Step [80/99], Loss: 0.1289\n",
      "Epoch [81/100], Step [20/99], Loss: 0.3709\n",
      "Epoch [81/100], Step [40/99], Loss: 0.4154\n",
      "Epoch [81/100], Step [60/99], Loss: 0.6106\n",
      "Epoch [81/100], Step [80/99], Loss: 0.2437\n",
      "Epoch [82/100], Step [20/99], Loss: 0.5103\n",
      "Epoch [82/100], Step [40/99], Loss: 0.4211\n",
      "Epoch [82/100], Step [60/99], Loss: 0.6382\n",
      "Epoch [82/100], Step [80/99], Loss: 1.0296\n",
      "Epoch [83/100], Step [20/99], Loss: 0.2228\n",
      "Epoch [83/100], Step [40/99], Loss: 0.4134\n",
      "Epoch [83/100], Step [60/99], Loss: 0.2403\n",
      "Epoch [83/100], Step [80/99], Loss: 0.3407\n",
      "Epoch [84/100], Step [20/99], Loss: 0.8565\n",
      "Epoch [84/100], Step [40/99], Loss: 0.5141\n",
      "Epoch [84/100], Step [60/99], Loss: 0.2008\n",
      "Epoch [84/100], Step [80/99], Loss: 0.6758\n",
      "Epoch [85/100], Step [20/99], Loss: 0.1485\n",
      "Epoch [85/100], Step [40/99], Loss: 1.6124\n",
      "Epoch [85/100], Step [60/99], Loss: 0.1687\n",
      "Epoch [85/100], Step [80/99], Loss: 0.3430\n",
      "Epoch [86/100], Step [20/99], Loss: 0.2443\n",
      "Epoch [86/100], Step [40/99], Loss: 0.5766\n",
      "Epoch [86/100], Step [60/99], Loss: 0.1696\n",
      "Epoch [86/100], Step [80/99], Loss: 0.2281\n",
      "Epoch [87/100], Step [20/99], Loss: 0.0773\n",
      "Epoch [87/100], Step [40/99], Loss: 0.5214\n",
      "Epoch [87/100], Step [60/99], Loss: 0.2008\n",
      "Epoch [87/100], Step [80/99], Loss: 0.3149\n",
      "Epoch [88/100], Step [20/99], Loss: 0.5800\n",
      "Epoch [88/100], Step [40/99], Loss: 0.5449\n",
      "Epoch [88/100], Step [60/99], Loss: 0.1721\n",
      "Epoch [88/100], Step [80/99], Loss: 0.1859\n",
      "Epoch [89/100], Step [20/99], Loss: 0.7920\n",
      "Epoch [89/100], Step [40/99], Loss: 0.0913\n",
      "Epoch [89/100], Step [60/99], Loss: 0.4915\n",
      "Epoch [89/100], Step [80/99], Loss: 0.6191\n",
      "Epoch [90/100], Step [20/99], Loss: 0.1610\n",
      "Epoch [90/100], Step [40/99], Loss: 0.2647\n",
      "Epoch [90/100], Step [60/99], Loss: 0.5604\n",
      "Epoch [90/100], Step [80/99], Loss: 0.0751\n",
      "Epoch [91/100], Step [20/99], Loss: 0.5794\n",
      "Epoch [91/100], Step [40/99], Loss: 0.5041\n",
      "Epoch [91/100], Step [60/99], Loss: 0.7611\n",
      "Epoch [91/100], Step [80/99], Loss: 0.5223\n",
      "Epoch [92/100], Step [20/99], Loss: 0.5452\n",
      "Epoch [92/100], Step [40/99], Loss: 0.3002\n",
      "Epoch [92/100], Step [60/99], Loss: 0.1290\n",
      "Epoch [92/100], Step [80/99], Loss: 0.7615\n",
      "Epoch [93/100], Step [20/99], Loss: 0.1737\n",
      "Epoch [93/100], Step [40/99], Loss: 0.5627\n",
      "Epoch [93/100], Step [60/99], Loss: 0.0481\n",
      "Epoch [93/100], Step [80/99], Loss: 0.4850\n",
      "Epoch [94/100], Step [20/99], Loss: 0.6852\n",
      "Epoch [94/100], Step [40/99], Loss: 0.3195\n",
      "Epoch [94/100], Step [60/99], Loss: 1.1590\n",
      "Epoch [94/100], Step [80/99], Loss: 0.9420\n",
      "Epoch [95/100], Step [20/99], Loss: 0.5631\n",
      "Epoch [95/100], Step [40/99], Loss: 1.3060\n",
      "Epoch [95/100], Step [60/99], Loss: 0.3930\n",
      "Epoch [95/100], Step [80/99], Loss: 0.1053\n",
      "Epoch [96/100], Step [20/99], Loss: 0.4908\n",
      "Epoch [96/100], Step [40/99], Loss: 0.1455\n",
      "Epoch [96/100], Step [60/99], Loss: 0.1091\n",
      "Epoch [96/100], Step [80/99], Loss: 0.9111\n",
      "Epoch [97/100], Step [20/99], Loss: 0.0740\n",
      "Epoch [97/100], Step [40/99], Loss: 0.2517\n",
      "Epoch [97/100], Step [60/99], Loss: 0.2540\n",
      "Epoch [97/100], Step [80/99], Loss: 0.5508\n",
      "Epoch [98/100], Step [20/99], Loss: 0.2884\n",
      "Epoch [98/100], Step [40/99], Loss: 0.3521\n",
      "Epoch [98/100], Step [60/99], Loss: 0.5384\n",
      "Epoch [98/100], Step [80/99], Loss: 0.1192\n",
      "Epoch [99/100], Step [20/99], Loss: 0.5092\n",
      "Epoch [99/100], Step [40/99], Loss: 0.2371\n",
      "Epoch [99/100], Step [60/99], Loss: 0.0740\n",
      "Epoch [99/100], Step [80/99], Loss: 0.1696\n",
      "Epoch [100/100], Step [20/99], Loss: 0.1598\n",
      "Epoch [100/100], Step [40/99], Loss: 0.9013\n",
      "Epoch [100/100], Step [60/99], Loss: 0.2270\n",
      "Epoch [100/100], Step [80/99], Loss: 0.4432\n",
      "训练完成\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 训练模型\n",
    "total_step = len(loader)\n",
    "for epoch in range(num_epochs):\n",
    "    for i, (sequences, labels) in enumerate(loader):\n",
    "        # print(i, (sequences, labels))\n",
    "        # input()\n",
    "        sequences = sequences.view(-1, len(sequences[0]), input_size).float().to(device) # 将输入形状调整为(batch_size, sequence_length, input_size)\n",
    "        labels = labels.to(device)\n",
    "\n",
    "        # 正向传播\n",
    "        outputs = model(sequences)\n",
    "        loss = criterion(outputs, labels)\n",
    "\n",
    "        # 反向传播和优化\n",
    "        optimizer.zero_grad()\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "\n",
    "        if (i+1) % 20 == 0:\n",
    "            print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, total_step, loss.item()))\n",
    "\n",
    "print('训练完成')\n",
    "\n",
    "# 在测试集上评估模型\n",
    "# ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4c2bf0a4-509e-42d5-b778-58ac71b35020",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载测试数据集\n",
    "test_dataset = CustomDataset('/root/LSTM2/test2.csv')  # 使用测试集的CSV文件路径\n",
    "# test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)\n",
    "test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False, collate_fn=collate_fn)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "fe2dec3d-c556-4473-bcbb-173cbc203941",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在测试集上的准确率: 86.67%\n"
     ]
    }
   ],
   "source": [
    "# 将模型设置为评估模式\n",
    "model.eval()\n",
    "\n",
    "total_correct = 0\n",
    "total_samples = 0\n",
    "with torch.no_grad():\n",
    "    for sequences, labels in test_loader:\n",
    "        sequences = sequences.view(-1, len(sequences[0]), input_size).float().to(device) # 将输入形状调整为(batch_size, sequence_length, input_size)\n",
    "        # sequences = sequences.unsqueeze(2).to(device)  # 添加一个维度，将序列变成三维张量\n",
    "        labels = labels.to(device)\n",
    "\n",
    "        # 正向传播\n",
    "        outputs = model(sequences)\n",
    "        # probabilities = F.softmax(outputs, dim=1)  # 应用softmax变换\n",
    "        _, predicted = torch.max(outputs, 1)\n",
    "\n",
    "        # 统计正确预测的样本数量\n",
    "        total_correct += (predicted == labels).sum().item()\n",
    "        total_samples += labels.size(0)\n",
    "\n",
    "# 计算准确率\n",
    "accuracy = total_correct / total_samples\n",
    "print('在测试集上的准确率: {:.2f}%'.format(accuracy * 100))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "28377cf5-1cd5-4411-b78b-f0402787b9d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "预测的类别索引为: 0\n"
     ]
    }
   ],
   "source": [
    "# 假设您有一个名为 sequence_to_predict 的序列，它是一个列表\n",
    "sequence_to_predict = [4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]\n",
    "\n",
    "# 转换成 PyTorch 张量，并添加批次维度\n",
    "sequence_tensor = torch.tensor(sequence_to_predict).unsqueeze(0).unsqueeze(-1).float().to(device)\n",
    "\n",
    "# 使用模型进行预测\n",
    "with torch.no_grad():\n",
    "    # 将模型设置为评估模式\n",
    "    model.eval()\n",
    "    # 进行预测\n",
    "    output = model(sequence_tensor)\n",
    "    # probabilities = F.softmax(outputs, dim=1)  # 应用softmax变换\n",
    "    \n",
    "    #print(outputs)\n",
    "    # 获取预测结果\n",
    "    _, predicted_class = torch.max(outputs, 1)\n",
    "\n",
    "# predicted_class 包含了模型预测的类别索引，您可以根据实际情况进行解释\n",
    "print(\"预测的类别索引为:\", predicted_class.item())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07a3cfb2-caaa-4b63-b363-a32fb8c587ab",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a62af889-090f-433d-87ad-ab651fd692db",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
